export function showTipsToast(message, duration = 2000) {
  uni.hideToast()
  return new Promise(resolve => {
    uni.showToast({
      icon: 'none',
    title: message,
    mask: true,
    duration,
      success: () => {
        setTimeout(() => {
          resolve()
        }, duration)
      }
    })
  })
}

export function showErrorToast(message, duration = 2000) {
  uni.hideToast()
  return new Promise(resolve => {
    uni.showToast({
      icon: 'none',
      image: '/static/error.png',
      title: message,
      mask: true,
      duration,
      success: () => {
        setTimeout(() => {
          resolve()
        }, duration)
      }
    })
  })
}

export function showSuccessToast(message, duration = 2000) {
  uni.hideToast()
  return new Promise(resolve => {
    uni.showToast({
      title: message,
      mask: true,
      duration,
      success: () => {
        setTimeout(() => {
          resolve()
        }, duration)
      }
    })
  })
}

export function showModal({title = '提示', content, showCancel = true, ...other}) {
  return new Promise(resolve => {
    uni.showModal({
      title,
      content,
      showCancel,
      ...other
    })
  })
}

export function showLoading() {
  uni.hideLoading()
  uni.showLoading({
    title: '加载中...',
    mask: true
  })
}

export function hideLoading(time = 2000) {
  setTimeout(() => {
    uni.hideLoading()
  }, time)
}

export function compressImage(filePath, quality = 50) {
  console.log('压缩图片:', filePath)
  return new Promise(resolve => {
    plus.zip.compressImage(
      {
        src: filePath,
        dst: filePath,
        overwrite: true,
        quality: quality,
        format: 'jpg',
        width: '800px'
      },
      res => {
        console.log('压缩成功', res)
        let targetPath = res.target
        resolve(targetPath)
        // const { size } = res
        // if (size > 3000000) {
        //   targetPath = compressImage(res.target)
        // } else {
        //   resolve(targetPath)
        // }
      },
      err => {
        console.log('压缩失败', err)
      }
    )
  })
}